package com.ruoyi.project.jiaqin.companyenvironment.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.project.jiaqin.company.domain.Company;
import com.ruoyi.project.jiaqin.company.service.ICompanyService;
import com.ruoyi.project.jiaqin.companycarousel.domain.CompanyCarousel;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.jiaqin.companyenvironment.domain.CompanyEnvironment;
import com.ruoyi.project.jiaqin.companyenvironment.service.ICompanyEnvironmentService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 企业环境Controller
 *
 * @author admin
 * @date 2021-01-08
 */
@Controller
@RequestMapping("/jiaqin/companyenvironment")
public class CompanyEnvironmentController extends BaseController
{
    private String prefix = "jiaqin/companyenvironment";

    @Autowired
    private ICompanyEnvironmentService companyEnvironmentService;
    @Autowired
    private ICompanyService iCompanyService;
    @RequiresPermissions("jiaqin:companyenvironment:view")
    @GetMapping()
    public String companyenvironment()
    {
        return prefix + "/companyenvironment";
    }

    /**
     * 查询企业环境列表
     */
    @RequiresPermissions("jiaqin:companyenvironment:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CompanyEnvironment companyEnvironment)
    {
        Company company = iCompanyService.selectCompanyBySysUserId(getSysUser().getUserId());
        if(company != null) {
            companyEnvironment.setCompanyId(company.getId());
            startPage();
            List<CompanyEnvironment> list = companyEnvironmentService.selectCompanyEnvironmentList(companyEnvironment);
            return getDataTable(list);
        }
        if(getSysUser().isAdmin()){
            startPage();
            List<CompanyEnvironment> list = companyEnvironmentService.selectCompanyEnvironmentList(companyEnvironment);
            return getDataTable(list);
        }
        return new TableDataInfo();
    }

    /**
     * 导出企业环境列表
     */
    @RequiresPermissions("jiaqin:companyenvironment:export")
    @Log(title = "企业环境", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CompanyEnvironment companyEnvironment)
    {
        List<CompanyEnvironment> list = companyEnvironmentService.selectCompanyEnvironmentList(companyEnvironment);
        ExcelUtil<CompanyEnvironment> util = new ExcelUtil<CompanyEnvironment>(CompanyEnvironment.class);
        return util.exportExcel(list, "companyenvironment");
    }

    /**
     * 新增企业环境
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        Company company = iCompanyService.selectCompanyBySysUserId(getSysUser().getUserId());
        if(company!=null){
            mmap.put("company",company);
            return prefix + "/addme";
        }
        return prefix + "/add";
    }

    /**
     * 新增保存企业环境
     */
    @RequiresPermissions("jiaqin:companyenvironment:add")
    @Log(title = "企业环境", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CompanyEnvironment companyEnvironment)
    {
        companyEnvironment.setCreateAt(new Date());
        return toAjax(companyEnvironmentService.insertCompanyEnvironment(companyEnvironment));
    }

    /**
     * 修改企业环境
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        CompanyEnvironment companyEnvironment = companyEnvironmentService.selectCompanyEnvironmentById(id);
        mmap.put("companyEnvironment", companyEnvironment);
        return prefix + "/edit";
    }

    /**
     * 修改保存企业环境
     */
    @RequiresPermissions("jiaqin:companyenvironment:edit")
    @Log(title = "企业环境", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CompanyEnvironment companyEnvironment)
    {
        return toAjax(companyEnvironmentService.updateCompanyEnvironment(companyEnvironment));
    }

    /**
     * 删除企业环境
     */
    @RequiresPermissions("jiaqin:companyenvironment:remove")
    @Log(title = "企业环境", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(companyEnvironmentService.deleteCompanyEnvironmentByIds(ids));
    }
}
